//
//  ScanIDCardFontViewController.m
//  NewPro
//
//  Created by RYX on 2018/10/19.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "ScanIDCardFontViewController.h"
#import "WYIDScanViewController.h"
#import "WYScanResultModel.h"
#import "ScanIdCardSuccessViewController.h"
#import "ScanIdList.h"
#import "ScanIdModel.h"
#import "RealNameParametersModel.h"

@interface ScanIDCardFontViewController ()
{
    
    __weak IBOutlet UIImageView *fontImView;
    
}
@property (nonatomic,strong) UIView *headerView;
@property (nonatomic,weak) UIButton *btn;

@end

@implementation ScanIDCardFontViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setNavBarWithTitle:@"实名认证"];
    
}



- (IBAction)onScanIDFontBtn:(UIButton *)sender {
    
    sender.enabled = NO;
    
    _btn = sender;
    
    MJWeakSelf
    if ([sender.currentTitle isEqualToString:@"识别身份证正面"]) {
        
        WYIDScanViewController *VC = [[WYIDScanViewController alloc] initWithCarInfo: CardIDFront];
        [VC scanDidFinishCarInfo:^(CardType status, WYScanResultModel *scanModel) {
            
            [ScanIdList sharedInstance].scanModel.code = scanModel.code;
            [ScanIdList sharedInstance].scanModel.name = scanModel.name;
            [ScanIdList sharedInstance].scanModel.gender = scanModel.gender;
            [ScanIdList sharedInstance].scanModel.nation = scanModel.nation;
            [ScanIdList sharedInstance].scanModel.address = scanModel.address;
            [ScanIdList sharedInstance].scanModel.idFontImage = scanModel.idImage;
            
            [ScanIdList sharedInstance].realNameModel.idCardNumber = scanModel.code;
            [ScanIdList sharedInstance].realNameModel.name = scanModel.name;
            [ScanIdList sharedInstance].realNameModel.idCardFrontImgData = [ImgManager imageToString:scanModel.idImage] ;
            
            [SVPManager showSuccessWithTitle:@"扫描成功，请扫描身份证背面"];
            self->fontImView.image = scanModel.idImage;
            [sender setTitle:@"识别身份证背面" forState:UIControlStateNormal];
            
        }];
        
        [VC scanFailure:^{
           
            sender.enabled = YES;
            [SVPManager showErrorWithTitle:@"请扫描身份证正面"];
        }];
        
        [self.navigationController pushViewController:VC animated:YES];
    }
    
    if ([sender.currentTitle isEqualToString:@"识别身份证背面"]) {
        
        WYIDScanViewController *VC = [[WYIDScanViewController alloc] initWithCarInfo: CardIDDown];
        
        [VC scanDidFinishCarInfo:^(CardType status, WYScanResultModel *scanModel) {
            
            sender.enabled = NO;
            
            [SVPManager showSuccessWithTitle:@"   扫 描 成 功   "];
            
            [ScanIdList sharedInstance].scanModel.valid = scanModel.valid;
            
            [ScanIdList sharedInstance].scanModel.idBackImage = scanModel.idImage;
            
            [ScanIdList sharedInstance].realNameModel.idCardBackImgData = [ImgManager imageToString:scanModel.idImage];
        
            //GCD延迟
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                ScanIdCardSuccessViewController *VC = [[ScanIdCardSuccessViewController alloc] init];
                [weakSelf.navigationController pushViewController:VC animated:YES];
                
            });
            
            
        }];
        
        [VC scanFailure:^{
            
            sender.enabled = YES;
            [SVPManager showErrorWithTitle:@"请扫描身份证反面"];
        }];
        
        [self.navigationController pushViewController:VC animated:YES];
    }
    
    
}


- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    _btn.enabled = YES;
}

@end
